package com.qfedu.hotelssm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.qfedu.hotelssm.dao.CtDailyDao;
import com.qfedu.hotelssm.dto.CtDailyDto;
import com.qfedu.hotelssm.entity.CtDaily;
import com.qfedu.hotelssm.service.intf.CtDailyService;
import com.qfedu.hotelssm.util.StringUtil;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author dzl
 * @date 2022/8/29 19:14
 */
@Service
public class CtDailyServiceImpl implements CtDailyService {
    @Autowired
    private CtDailyDao dao;
    @Override
    public R save(CtDaily daily) {
        if (dao.save(daily)>0){
            return R.ok("恭喜你，新增成功了");
        }else{
            return R.fail("亲，新增失败了");
        }

    }

    @Override
    public R del(int id) {
        if (dao.del(id)>0){
            return R.ok("恭喜你，删除成功了");
        }else{
            return R.fail("亲，删除失败了");
        }

    }

    @Override
    public PageBean page(String info, int page, int limit) {
        if (StringUtil.isNotEmpty(info)){
            info="%"+info+"%";
        }
        int start=(page-1)*limit;
        return new PageBean((int) dao.selectCount(info),dao.selectPage(info, start, limit));
    }

    @Override
    public R update(CtDaily daily) {
        if (dao.update(daily)>0){
            return R.ok("恭喜你，修改成功了");
        }else{
            return R.fail("亲，修改失败了");
        }
    }
        //查询所有日报
    @Override
    public List<CtDailyDto> all() {
        return dao.all();
    }

        //根据id批量删除
    @Override
    public R delIds(int[] ids){
        if (dao.delByids(ids)>0){
        return R.ok();
        }else {
            return R.fail();
        }
    }

        //Execl导入导出
    @Override
    public R upload(MultipartFile file) throws IOException {
        //1.接收文件，收取文件内容
        //1.验证上传的文件是否存在
        if (!file.isEmpty()){
            //2.解析Execl--EasyExcel
            //2.解析Execl数据
            List<CtDaily> list= EasyExcel.read(file.getInputStream(), CtDaily.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {

                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).sheet().doReadSync();
            //3.实现mybatis 实现批量新增
                if (dao.saveBatch(list)>0) {
                    //4.返回
                    System.err.println("**********");
                    System.err.println(list);
                    return R.ok();
                }
        }
            return R.fail();
    }
}
